package com.test.webtest.controller;

import com.test.webtest.dto.Result;
import com.test.webtest.entity.Sort;
import com.test.webtest.utils.BubbleSortUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description SortController
 * @Author jiangzhenghao
 * @Date 2025-05-16  17:48
 */

@RestController
public class SortController {

    /**
     * 冒泡排序
     * @param sort 包含数组和排序方向的请求体
     * @return 排序结果
     */
    @PostMapping("/sort")
    public Result sortArray(@RequestBody Sort sort) {
        try {
            int[] sorted = BubbleSortUtil.bubbleSort(sort.getNumbers(), sort.isAscending());
            return Result.ok(sorted);
        } catch (Exception e) {
            return Result.fail("排序失败");
        }
    }
}
